<script>
	import {mapState,  mapMutations} from 'vuex'; 
	export default {
		onLaunch: function(options) {
			uni.hideTabBar();
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
			});

			updateManager.onUpdateReady(function(res) {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					}
				});

			});

			updateManager.onUpdateFailed(function(res) {
				// 新的版本下载失败
			});
			
			let token = uni.getStorageSync("token");
			this.initialize(options.scene);
			if(token) {
				this.setLogin(true);
				this.getUserCenter();
			}
		},
		onShow: function() {
			
		},
		onHide: function() {
			// console.log('App Hide');
		},
		methods:{
			...mapMutations(['setInitialize', 'setLogin', 'setTodayIsSign', 'setCos', 'setCosBucket', 'setCosRegion', 'setScene', 'setUserInfo', 'setContact']),
			async initialize(scene) {
				let res = await this.$api('initialize')
				let tokenInvalid = res.data.tokenInvalid;
				if(tokenInvalid) {
					this.loginOut();
				}
				this.setInitialize(res.data.initialize);
				this.setScene(scene);
			},
			async getUserCenter() {
				let res = await this.$api('center')
				this.setUserInfo(res.data.userInfo);
				this.setContact(res.data.contact);
				this.setTodayIsSign(res.data.todayIsSign);
				uni.setStorageSync("userInfo", res.data.userInfo);
				uni.setStorageSync("contact", res.data.contact);
			},
			loginOut() {
				this.setLogin(false);
				this.setUserInfo("");
				this.setContact("");
				this.setTodayIsSign("");
				uni.removeStorageSync("userInfo");
				uni.removeStorageSync("contact");
			}
		}
	};
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import "static/css/iconfont.css";
	@import 'static/css/common.scss';
</style>
